package cn.iocoder.yudao.module.biz.controller.admin.salesteam;

import cn.iocoder.yudao.module.biz.controller.admin.salesteam.vo.BizSalesTeamPageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.salesteam.vo.BizSalesTeamRespVO;
import cn.iocoder.yudao.module.biz.controller.admin.salesteam.vo.BizSalesTeamSaveReqVO;
import cn.iocoder.yudao.module.biz.dal.dataobject.bizsalesteam.BizSalesTeamDO;
import cn.iocoder.yudao.module.biz.service.bizsalesteam.BizSalesTeamService;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 销售团队信息")
@RestController
@RequestMapping("/saleTeam/biz-sales-team")
@Validated
public class BizSalesTeamController {

    @Resource
    private BizSalesTeamService bizSalesTeamService;

    @PostMapping("/create")
    @Operation(summary = "创建销售团队信息")
    @PreAuthorize("@ss.hasPermission('saleTeam:biz-sales-team:create')")
    public CommonResult<Long> createBizSalesTeam(@Valid @RequestBody BizSalesTeamSaveReqVO createReqVO) {
        return success(bizSalesTeamService.createBizSalesTeam(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新销售团队信息")
    @PreAuthorize("@ss.hasPermission('saleTeam:biz-sales-team:update')")
    public CommonResult<Boolean> updateBizSalesTeam(@Valid @RequestBody BizSalesTeamSaveReqVO updateReqVO) {
        bizSalesTeamService.updateBizSalesTeam(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除销售团队信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('saleTeam:biz-sales-team:delete')")
    public CommonResult<Boolean> deleteBizSalesTeam(@RequestParam("id") Integer id) {
        bizSalesTeamService.deleteBizSalesTeam(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除销售团队信息")
                @PreAuthorize("@ss.hasPermission('saleTeam:biz-sales-team:delete')")
    public CommonResult<Boolean> deleteBizSalesTeamList(@RequestParam("ids") List<Integer> ids) {
        bizSalesTeamService.deleteBizSalesTeamListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得销售团队信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('saleTeam:biz-sales-team:query')")
    public CommonResult<BizSalesTeamRespVO> getBizSalesTeam(@RequestParam("id") Long id) {
        BizSalesTeamDO bizSalesTeam = bizSalesTeamService.getBizSalesTeam(id);
        bizSalesTeam.setUsers(bizSalesTeamService.getUsers(bizSalesTeam.getTeamId()));
        return success(BeanUtils.toBean(bizSalesTeam, BizSalesTeamRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得销售团队信息分页")
    @PreAuthorize("@ss.hasPermission('saleTeam:biz-sales-team:query')")
    public CommonResult<PageResult<BizSalesTeamRespVO>> getBizSalesTeamPage(@Valid BizSalesTeamPageReqVO pageReqVO) {
        PageResult<BizSalesTeamDO> pageResult = bizSalesTeamService.getBizSalesTeamPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BizSalesTeamRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出销售团队信息 Excel")
    @PreAuthorize("@ss.hasPermission('saleTeam:biz-sales-team:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBizSalesTeamExcel(@Valid BizSalesTeamPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BizSalesTeamDO> list = bizSalesTeamService.getBizSalesTeamPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "销售团队信息.xls", "数据", BizSalesTeamRespVO.class,
                        BeanUtils.toBean(list, BizSalesTeamRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得销售团队信息不分页")
    public CommonResult<List<BizSalesTeamDO>> getBizSalesTeamPageList(@Valid BizSalesTeamPageReqVO pageReqVO) {
        PageResult<BizSalesTeamDO> pageResult = bizSalesTeamService.getBizSalesTeamPage(pageReqVO);
        return success(pageResult.getList());
    }

    @GetMapping("/getBizSalesTeamUser")
    @Operation(summary = "查询已经分组的用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<AdminUserDO>> getBizSalesTeamUser(@RequestParam("id") Long id) {
        List<AdminUserDO> list = bizSalesTeamService.getBizSalesTeamUser(id);
        return success(list);
    }
}
